#include "Vault.h"

struct _VAULT * Vault_Create()
{
	struct _VAULT * V;
	V = (struct _VAULT *)malloc(sizeof(struct _VAULT));
	V->Building = Security_Create();
	V->NumberOfLocks = 0;
	return V;
}

void Vault_Load_Init(struct _VAULT * V)
{
	struct _SECURITY * cursor = V->Building;
	while(cursor)
	{
		cursor->Info->Encrypted = TRUE;
		cursor->Time->Encrypted = TRUE;
		cursor = cursor->Next;
	}
}

void Vault_Enter_Book(struct _VAULT * V,struct _Book * B)
{
	for(V->CurrentLock = V->Building,B->CurrentPage = B->Pages;
		B->CurrentPage;
		V->NumberOfLocks++
		)
	{
		Security_Encode(V->CurrentLock,B->CurrentPage->m_BStamp,B->CurrentPage->m_BText);
		if(B->CurrentPage->NextPage)
		{
			V->CurrentLock->Next = Security_Create_Linked(NULL,V->CurrentLock);
			V->CurrentLock = V->CurrentLock->Next;
		}
		B->CurrentPage = B->CurrentPage->NextPage;
	}
}

struct _Book * Vault_Return_Book(struct _VAULT * V)
{
	struct _Book * B;
	struct _SECURITY * cursor = V->Building;
	B = Book_Create();
	while(cursor)
	{
		if(cursor->Encoded)
			Book_Add_Entry_Time(B,cursor->Info->D,cursor->Time->D);
		cursor = cursor->Next;
	}
	return B;
}

//TODO: Finish
void Vault_Enter_Data(struct _VAULT * V,struct _BUFFER * Info,int InfoKey,struct _BUFFER * Time,int TimeKey)
{
	struct _SECURITY * S, * cursor = V->Building;
	S = Security_Create();
	Security_Encode(S, Time , Info);
	S->Info->K = InfoKey;
	S->Time->K = TimeKey;
	if(!cursor->Encoded){V->Building = S;return;}
	while(cursor)
	{
		if(!cursor->Next)
		{
			cursor->Next = S;
			S->Prev = cursor;
			return;
		}
		cursor = cursor->Next;
	}
}

void Vault_Encrypt(struct _VAULT * V)
{
	struct _SECURITY * cursor = V->Building;
	while(cursor)
	{
		Security_Encrypt(cursor);
		cursor = cursor->Next;
	}
}

void Vault_Decrypt(struct _VAULT * V)
{
	struct _SECURITY * cursor = V->Building;
	while(cursor)
	{
		Security_Decrypt(cursor);
		cursor = cursor->Next;
	}
}